const axios = require('axios')
const cheerio = require('cheerio')
const iconv = require('iconv-lite')
const pool = require('../utils/utils.js')

module.exports = async(ctx, next) => {
    var url = ctx.query.url
    var bookinfo = await new Promise((resolve, reject) => {
        var book = {}
        axios({
            method: 'get',
            url: url,
            responseType: 'stream'
        }).then(res => {
            var stream = []
            res.data.on('data', st => {
                stream.push(st)
            })
            res.data.on('end', () => {
                let buffer = Buffer.concat(stream)
                let domStr = iconv.decode(buffer, 'utf8')
                var $ = cheerio.load(domStr)
                book.url = $('meta[property="og:url"]').attr('content')
                book.bookName = $('#info h1').text()
                book.author = $('#info p').eq(0).text().slice(7)
                book.intro = $('#intro p').eq(0).text()
                book.category = $('.con_top a').eq(1).text()
                book.status = $('#info p').eq(1).text().slice(7, 10)
                book.updateTime = $('#info p').eq(2).text().slice(5)
                book.lastChapterName = $('#info p').eq(3).find('a').text()
                book.lastChapterUrl = 'https://www.81book.com' + $('#info p').eq(3).find('a').attr('href')
                book.picUrl = $('#fmimg img').attr('src')

                // INSERT INTO `rank` VALUES (),为啥要这样才能插入数据，卧槽
                var queryStr = `insert into ` + "`book`" + ` values ('${book.url}','${book.bookName}','${book.intro}','${book.category}','${book.status}','${book.author}','${book.lastChapterName}','${book.lastChapterUrl}','${book.picUrl}','${book.updateTime}')`;
                // console.log(queryStr)
                pool.query(queryStr, (err, result) => {
                    if (err) {
                        // console.log('存储失败' + err)
                    } else {
                        // console.log('存储成功')
                    }
                })
                resolve(book)
            })
        }).catch(err => {
            // console.log(err)
        })
    })
    ctx.body = bookinfo
}